package ytdp.careagle.com.ytdp.fragment;

import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.app.APP;
import ytdp.careagle.com.ytdp.app.G;
import ytdp.careagle.com.ytdp.app.manager.ImageLoadManager;
import ytdp.careagle.com.ytdp.entity.SellCarProgramEntity;
import ytdp.careagle.com.ytdp.myview.MyVideoView;

public class FragmentVideoViewpagerFragment extends BaseFragment implements MyVideoView.PlayPauseListener {

    @Bind(R.id.iv_car_img)
    ImageView ivCarImg;
    @Bind(R.id.myVideoView)
    MyVideoView myVideoView;
    @Bind(R.id.iv_play)
    ImageView ivPlay;
    @Bind(R.id.rl_gen)
    RelativeLayout rlGen;
    private boolean isPlay = true;
    private SellCarProgramEntity.MultimediaBean data;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_video_viewpager, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        APP.getInstance().resetCountDown();
        data = (SellCarProgramEntity.MultimediaBean) getArguments().getSerializable("data");
        ImageLoadManager.getImageLoadManager().load(data.getSourceUrl(), ivCarImg);
        if (data.getSourceType().equals("SOURCE_IMAGE")) {
            myVideoView.setVisibility(View.GONE);
            ivPlay.setVisibility(View.GONE);
        } else {
            Uri uri = Uri.parse(data.getSourceVideoUrl());
            MediaController mc = new MediaController(getActivity());
            mc.setVisibility(View.INVISIBLE);  //隐藏VideoView自带的进度条
            myVideoView.setMediaController(mc);
//video.setVideoPath(VideoPath);//播放本地视频
            myVideoView.setVideoURI(uri);//播放网络视频
            myVideoView.requestFocus();
            myVideoView.setPlayPauseListener(this);
        }
    }

    @OnClick({R.id.iv_play, R.id.myVideoView, R.id.rl_gen})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_play:
                myVideoView.start();
                break;
            case R.id.rl_gen:
                if (data == null || data.getSourceType().equals("SOURCE_IMAGE")) {
                    return;
                }
                if (isPlay) {
                    stopVideo();
                } else {
                    startVideo();
                }
                break;
            case R.id.myVideoView:
                myVideoView.pause();
                break;

        }
    }

    public void stopVideo() {
        if (data == null || data.getSourceType().equals("SOURCE_IMAGE")) {
            return;
        }
        if (myVideoView != null) {
            myVideoView.pause();
        }
    }

    public void startVideo() {
        if (data == null || data.getSourceType().equals("SOURCE_IMAGE")) {
            return;
        }
        if (myVideoView != null) {
            myVideoView.start();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onVideoPlay() {
        isPlay = true;
        ivPlay.setVisibility(View.GONE);
        ivCarImg.setVisibility(View.GONE);
        APP.getInstance().resetCountDown();
    }

    @Override
    public void onVideoPause() {
        isPlay = false;
        ivPlay.setVisibility(View.VISIBLE);
        APP.getInstance().resetCountDown();
    }
}
